package net.zjitc.service.Impl;

import net.zjitc.mapper.MovieMapper;
import net.zjitc.mapper.OrderItemMapper;
import net.zjitc.mapper.OrderMapper;
import net.zjitc.mapper.UserMapper;
import net.zjitc.pojo.Movie;
import net.zjitc.pojo.OrderItem;
import net.zjitc.pojo.Orders;
import net.zjitc.pojo.User;
import net.zjitc.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.UUID;

@Service
@Transactional
public class OrderServiceImpl implements OrderService{
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    OrderItemMapper orderItemMapper;
    @Autowired
    MovieMapper movieMapper;
    @Autowired
    UserMapper userMapper;

    @Override
    public List<Orders> findOrdersByUid(int id) {
        List<Orders> ordersList = orderMapper.findOrdersByUid(id);
        for (Orders o :ordersList
             ) {
            OrderItem orderItem = orderItemMapper.findMovieByOid(o.getId());
            Movie movie = movieMapper.findById(orderItem.getMovie_id());
            orderItem.setMovie(movie);
            o.setOrderItem(orderItem);
        }
        return ordersList;
    }

    @Override
    public List<Orders> findAll() {
        List<Orders>ordersList = orderMapper.findAll();
        for (Orders o :ordersList
             ) {
            User user = userMapper.findById(o.getUser_id());
            o.setUser(user);
        }
        return ordersList;
    }

    @Override
    public int createOrder(Orders orders) {
        orders.setId(UUID.randomUUID().toString());
        orders.setPaystate(0);
        return orderMapper.createOrder(orders);
    }

    @Override
    public int updatePayState(String out_trade_no, Integer state) {
        return orderMapper.updatePayState(out_trade_no,state);
    }
}
